package x.y.z;

import java.util.Arrays;

public class LeetCode_0135 {
    public int candy(int[] ratings) {
        int[] candies = new int[ratings.length];
        Arrays.fill(candies, 1);
        for (int i = 1; i < candies.length; i++) {
            if (ratings[i] > ratings[i - 1]) {
                candies[i] = candies[i - 1] + 1;
            }
        }
        for (int j = candies.length - 2; j >= 0; j--) {
            if (ratings[j] > ratings[j + 1]) {
                candies[j] = Math.max(candies[j], candies[j + 1] + 1);
            }
        }
        int result = 0;
        for (int candy : candies) {
            result += candy;
        }
        return result;
    }
}
